﻿
using BaseUtils.Logger;
using Flee.PublicTypes;
using System;
using System.Collections.Concurrent;

namespace BaseUtils.Utils
{
    public class CalculatorHelper
    {

        public static dynamic Computing(ConcurrentDictionary<string, object> keyValuePairs, string formula)
        {
            // 创建表达式上下文
            ExpressionContext context = new ExpressionContext();

            foreach (var item in keyValuePairs)
            {
                context.Variables[item.Key] = item.Value;
            }

            // 添加变量
            //context.Variables["GLOBAL_PARAM_1_ga_sn1"] = 5;
            //context.Variables["y"] = 3;
            // 创建表达式
            IGenericExpression<object> e = null;
            string _formula = formula.Replace("${", "").Replace("}", "");
            e = context.CompileGeneric<object>(_formula);
            dynamic resulet = e.Evaluate();

            return resulet;
        }

        public ExpressionContext GetExpressionContext()
        {
            return new ExpressionContext();
        }

    }
}
